<?php
require_once('functions.php');

function getCPhones()
{
	$conn = getConnection();
	return $conn->query('SELECT * FROM Customer_Phone');
}

function getCPhone($row)
{ 
		$conn = getConnection();
        $results = $conn->query("SELECT * FROM Customer_Phone WHERE user_ID='$row[user_ID]' AND type='$row[type]' AND phone='$row[phone]'");
        $row = $results->fetch_assoc();
        $conn->close();
        return $row;
}


function noPhone()
{
	$conn = getConnection();
	$sql = "SELECT u.user_ID, firstName, secondName FROM User u LEFT JOIN Customer_Phone cp ON u.user_ID = cp.user_ID WHERE cp.user_ID IS NULL";
	$results = $conn->query($sql);
    $conn->close();
       
    return $results;
}

function blankPhone($id)
{
	return array('user_ID' => $id, 'phone' => null, 'type' => null);
} 

function deletePhone($id, $type, $phone)
{
		$conn = getConnection();
		$conn->query("DELETE FROM Customer_Phone WHERE user_ID = '$id' AND type = '$type' AND phone = '$phone'");
		$conn->close();
		header('Location: index.php');
}

function savePhone($row)
{
        $conn = GetConnection();
        $row2 = escapeRow($row, $conn);
		
		if (getCPhone($row2))
        	$sql =  "UPDATE Customer_Phone SET type='$row2[newType]', phone='$row2[newPhone]' WHERE user_ID= '$row2[user_ID]' AND type='$row2[type]' AND phone='$row2[phone]'";
        else
        	$sql = "INSERT INTO Customer_Phone VALUES ('$row2[user_ID]', '$row2[newType]', '$row2[newPhone]')";
             
        $results = $conn->query($sql);
        $error = $conn->error;
        $conn->close();
       
        return $error == '' ? true : array('SQL Error' => $error);
}

	
